# -*- coding: utf-8 -*-

import torch.nn as nn
import torch

class LSTMCell(nn.Module):
    def __init__(self, input_size, hidden_size, cell_size, output_size):
        super(LSTMCell, self).__init__()
        self.hidden_size = hidden_size
        self.cell_size = cell_size
        self.gate = nn.Linear(input_size+hidden_size, cell_size)
        self.output = nn.Linear(hidden_size, output_size)
        self.sigmoid = nn.Sigmoid()
        self.tanh = nn.Tanh()
        self.softmax = nn.LogSoftmax(dim=1)
    
    def forward(self, input, hidden, cell):
        combined = torch.cat((input, hidden), 1) # 第二维度相加
        f_gate = self.sigmoid(self.gate(combined))
        i_gate = self.sigmoid(self.gate(combined))
        o_gate = self.sigmoid(self.gate(combined))
        z_state = self.sigmoid(self.gate(combined))
        cell = torch.add(torch.mul(cell,f_gate), torch.mul(z_state,i_gate))
        hidden = torch.mul(self.tanh(cell), o_gate)
        output = self.output(hidden)
        output = self.softmax(output)
        return output, hidden, cell
    
    def initHidden(self):
        return torch.zeros(1, self.hidden_size)
    
    def initCell(self):
        return torch.zeros(1, self.cell_size)

# 实例化
lstmcell = LSTMCell(input_size=10, hidden_size=20, cell_size=20, output_size=10)
input = torch.randn(32,10) # 序列长度为1
h_0 = torch.randn(32,20)
output, hn, cn = lstmcell(input, h_0, h_0) # 直接forward 
print(output.size(),hn.size(),cn.size())
